package com.wlcorp.forcazamis.dao.sample.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.sample.ISampleDAO;
import com.wlcorp.forcazamis.domain.sample.Sample;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.Validator;

public class SampleDAOImpl extends HibernateDaoSupport implements ISampleDAO {

	@SuppressWarnings("unchecked")
	public List getSampleList(final Sample sample) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select sample from Sample as sample where sample.deleteFlag=false and sample.flag != ").append(AppConstant.RECORD_DELETED);
		if(null != sample){
			if(Validator.checkValueIsValid(sample.getSampleCode())){
				queryString.append(" and sample.sampleCode like :sampleCode ");
			}
			if(null != sample.getSourceType()){
				queryString.append(" and sample.sourceType = ").append(sample.getSourceType());
			}
			if(Validator.checkValueIsValid(sample.getSource1())){
				queryString.append(" and sample.source1 like '%").append(sample.getSource1()).append("%'");
			}
			if(Validator.checkValueIsValid(sample.getSource2())){
				queryString.append(" and sample.source2 like '%").append(sample.getSource2()).append("%'");
			}
			if(Validator.checkValueIsValid(sample.getSource3())){
				queryString.append(" and sample.source3 like '%").append(sample.getSource3()).append("%'");
			}
			if(Validator.checkValueIsValid(sample.getFactoryCode())){
				queryString.append(" and sample.factoryCode like '%").append(sample.getFactoryCode()).append("%'");
			}
			if(Validator.checkValueIsValid(sample.getSourceCode())){
				queryString.append(" and sample.sourceCode like '%").append(sample.getSourceCode()).append("%'");
			}
			queryString.append(" order by sample.createdDate desc");
		}
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(Validator.checkValueIsValid(sample.getSampleCode())){
					query.setString("sampleCode","%"+sample.getSampleCode()+"%");
				}
				if(null != sample && null != sample.getMinRownum()){
					query.setFirstResult(sample.getMinRownum());
					query.setMaxResults(sample.getNumPerPage());
				}
				return query.list();
			}
			
		});
	}
	
	public Sample getSampleById(Long sampleId) throws DaoException {
		return (Sample)getHibernateTemplate().get(Sample.class,sampleId);
	}

	public void deleteSample(Sample sample) throws DaoException {
		sample.setDeleteFlag(true);
		getHibernateTemplate().update(sample);
	}

	public void saveSample(Sample sample) throws DaoException {
		getHibernateTemplate().saveOrUpdate(sample);
		getHibernateTemplate().flush();
		getHibernateTemplate().clear();
	}

	public Long getSampleCount(final Sample sample) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select count(sample) from Sample as sample where 1=1 and sample.deleteFlag=false");
		if(null != sample){
			if(Validator.checkValueIsValid(sample.getSampleCode())){
				queryString.append(" and sample.sampleCode like '%").append(sample.getSampleCode()).append("%'");
			}
			if(null != sample.getSourceType()){
				queryString.append(" and sample.sourceType = ").append(sample.getSourceType());
			}
			if(Validator.checkValueIsValid(sample.getSource1())){
				queryString.append(" and sample.source1 like '%").append(sample.getSource1()).append("%'");
			}
			if(Validator.checkValueIsValid(sample.getSource2())){
				queryString.append(" and sample.source2 like '%").append(sample.getSource2()).append("%'");
			}
			if(Validator.checkValueIsValid(sample.getSource3())){
				queryString.append(" and sample.source3 like '%").append(sample.getSource3()).append("%'");
			}
			if(Validator.checkValueIsValid(sample.getFactoryCode())){
				queryString.append(" and sample.factoryCode like '%").append(sample.getFactoryCode()).append("%'");
			}
			if(Validator.checkValueIsValid(sample.getSourceCode())){
				queryString.append(" and sample.sourceCode like '%").append(sample.getSourceCode()).append("%'");
			}
		}
		return (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				return query.uniqueResult();
			}
		});
	}

	public Long getSameTypeLatestSerialCode(final Sample sample) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select max(sample.serialCode) from Sample as sample where sample.deleteFlag=false");
			//.append(" where sample.sampleCode like '").append(sample.getSampleCode()).append("%'")
			//.append(" order by sample.serialCode desc");
		Object serialCode = getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				return query.uniqueResult();
			}
		});
		if(null == serialCode){
			return new Long(0);
		}
		return (Long)serialCode;
	}

	@SuppressWarnings("unchecked")
	public List getSampleByCode(Sample sample) throws DaoException {
		StringBuffer queryStr = new StringBuffer("select sample from Sample as sample where sample.deleteFlag=false and sample.sampleCode = '")
			.append(sample.getSampleCode()).append("'");
		return (List)getHibernateTemplate().find(queryStr.toString());
	}

}
